package com.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class ServiceFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        req.setCharacterEncoding("utf-8");
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        if (req.getServletPath().endsWith("html") || req.getServletPath().endsWith("htm")) {
            resp.setHeader("Content-type", "text/html;charset=utf-8");
        } else {
            resp.setHeader("Content-type", "text/json;charset=utf-8");
        }
        filterChain.doFilter(req, resp);
    }
}
